<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sales
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * Abstract API2 class for order items rest
 *
 * @category   Mage
 * @package    Mage_Sales
 * @author     Magento Core Team <core@magentocommerce.com>
 */
abstract class ApiCenter_SalesApi_Model_Api2_Orderapi_Orderwarehouse_Rest extends ApiCenter_SalesApi_Model_Api2_Orderapi_Orderwarehouse
{
    /**#@+
     * Parameters in request used in model (usually specified in route)
     */
    const PARAM_ORDER_ID = 'id';
    /**#@-*/
    const  DEFAULT_WIDTH = "100";
    /**#@-*/
    const  DEFAULT_HEIGHT = "100";
    
    const VENDOR_ID="vendor_id";
    
    const NOIMAGE ="";
    
    const  DEFAULT_FILTER_TYPE = "simple";
    
    
//     public $shipping_type =array(
//         "29"=>"14",
//         "10"=>"16",
//     );
   
    
    /**
     * Get order items list
     *
     * @return array
     */
    protected function _retrieve()
    {
       try{
        $incrementid    = $this->getRequest()->getParam('id');
        $vendorId  =$this->getRequest()->getParam('vendorid');
        
        $collection = $this->_getCollectionForRetrieve($incrementid,$vendorId);
      
      
        $orderData =$collection->getData();
       
        foreach ($orderData as $k => $v){
            if($v['v_send_bsp_status']!=="OK"){
                $items     = $this->_getItems(array($v["entity_id"]));
                $resData[$k] = $v;
                $resData[$k]['order_items'] = $items;
            }
           
        }

             $res['status'] =1;
            $res['info'] =$resData;
        } catch (Exception $e) {
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }
        return $res;
    }
    /**
     * Retrieve order items collection
     *
     * @return Mage_Sales_Model_Resource_Order_Item_Collection
     */
    protected function _getCollectionForRetrieve($incrementid,$vendorId)
    {
        /* @var $order Mage_Sales_Model_Order */
        $order = $this->_loadOrderByIncrementid($incrementid);
        /* @var $collection Mage_Sales_Model_Resource_Order_Item_Collection */
		$collection = Mage::getResourceModel('salesapi/order_collection');
        $collection->addFieldToFilter("vendor_id",$vendorId);
		$collection->addFieldToFilter("increment_id",array("like"=>$order->getIncrementId()."%"));
		$collection->getSelect()
		->joinleft(array('d' => 'tb_logistics_product'),
		    'd.proCode = main_table.v_carrier_product',
		    array(
		        "shipping_type_id"=>'d.proCode',
		        "shipping_type_en"=>"d.proName_en",
		        "shipping_type"=>"d.proName",
		    ));
        return $collection;
    }

    /**
     * Load order by id
     *
     * @param int $id
     * @throws Mage_Api2_Exception
     * @return Mage_Sales_Model_Order
     */
    protected function _loadOrderByIncrementid($incrementid)
    {
        /* @var $order Mage_Sales_Model_Order */
        $order = Mage::getModel('sales/order')->load($incrementid,"increment_id");
        
        if (!$order->getId()) {
            $this->_critical("订单未找到",400);
        }
        return $order;
    }
  
    
    protected  function _multiUpdate($filteredData){
        
        try {
            
            if(!is_array($filteredData)){
                $this->_critical("传输数据格式错误",400);
            }
           
            foreach ($filteredData as $k=>$_orderitem){
                    $orderModel = $this->_loadOrderInformation($_orderitem['order_id']);
                    if($orderModel->getVSendBspStatus()=="Ok"){
                        unset($filteredData[$k]);
                    }
            } 
            try {
                $resData = Mage::helper('saleorderservice/confirm')->confirmWarehouse($filteredData);
            } catch (Exception $e) {
                Mage::log("仓库确认地址接口错误",1,"api.log");
                $this->_critical("仓库地址确认错误，稍后重试",400);
            }

            $res['status']=1;
            $res['info']=$resData;
        } catch (Exception $e) {
            $res['status']=0;
            $res['info']=$e->getMessage();
        }
       return $res;
    }

    protected  function _loadOrderInformation($incrementId){
        $model = Mage::getModel("salesapi/order")->load($incrementId,"increment_id");
        return $model;
    }
}
